package com.iwhalecloud.bss.kite.cucc.service.pos.bcmc;

import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.inf.InfServContextDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosDTO;
import com.iwhalecloud.bss.kite.cucc.dataservice.cache.ExtOrderInfoCache;
import com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.dispatcher.BcmcOderDispatcherFactory;
import com.iwhalecloud.bss.kite.cucc.service.pos.bo.BcmcPosSceneBO;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.CuccPosServiceUtils;

@Service
public class BcmcPosServiceHandler implements IBcmcPosServiceHandler {

    @Override
    public KitePosDTO deal(InfServContextDTO infServContextDTO) {
        // 1.初始化转单报文上下文
        BcmcPosContext context = initContext(infServContextDTO);

        // 2.组装报文任务调度
        BcmcOderDispatcherFactory.invoke(context);

        // 3.返回报文对象
        return context.getPosDto();
    }

    /**
     * Description: <br>
     * 初始化上下文
     *
     * @author XXX<br>
     * @taskId <br>
     * @param infServContextDTO
     * @return <br>
     */
    private BcmcPosContext initContext(InfServContextDTO infServContextDTO) {
        BcmcPosContext context = KiteBeanUtils.transform(infServContextDTO, BcmcPosContext.class);
        context.setLanId(infServContextDTO.getCustDto().getLanId());
        context.setCustId(infServContextDTO.getCustDto().getCustId());

        String sceneInstId = infServContextDTO.getSceneInstId();
        BcmcPosSceneBO posSceneBO = new BcmcPosSceneBO();
        posSceneBO.setCustId(infServContextDTO.getCustDto().getCustId());
        posSceneBO.initPosData(sceneInstId);
        context.setCustDto(infServContextDTO.getCustDto());
        context.setPosSceneBO(posSceneBO);

        // 设置场景实例对象
        CuccPosServiceUtils.getSceneInst(context);

        // 从缓存取，签订和变更场景
        context.setPosDto(ExtOrderInfoCache.get(sceneInstId));
        return context;
    }

}
